﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace KWBridge
{
    public partial class LoginForm : Form
    {
        private KWBridgeForm mainForm;

        public LoginForm( KWBridgeForm mainForm , bool bModify )
        {
            InitializeComponent( );
            this.mainForm = mainForm;
            if ( CompanyInfo.IsOEM )
            {
                this.Text = CompanyInfo.Name + "登录窗口";
            }
            if ( bModify )
            {
                btnQuit.Visible = false;
            }
            else
            {
                btnModify.Visible = false;
            }
            tbUserId.KeyDown += new KeyEventHandler( tbUserId_KeyDown );
            tbPassword.KeyDown += new KeyEventHandler( tbPassword_KeyDown );
        }

        private void tbPassword_KeyDown( object sender , KeyEventArgs e )
        {
            if ( e.KeyData == Keys.Enter )
            {
                if ( btnQuit.Visible )
                {
                    btnQuit.Select( );
                }
                if ( btnModify.Visible )
                {
                    btnModify.Select( );
                }
            }
        }

        private void tbUserId_KeyDown( object sender , KeyEventArgs e )
        {
            if ( e.KeyData == Keys.Enter )
            {
                tbPassword.Select( );
            }
        }

        private void btnQuit_Click( object sender , EventArgs e )
        {
            if ( tbUserId.Text.ToLower( ) == "admin" && tbPassword.Text == "admin" )
            {
                if ( MessageBox.Show( this , "您想退出系统吗?" , "关闭提示" , MessageBoxButtons.YesNo , MessageBoxIcon.Information ) == DialogResult.Yes )
                {
                    //mainForm.ExitSys();
                    this.DialogResult = DialogResult.Yes;
                    Close( );
                }
            }
            else
            {
                MessageBox.Show( this , "您输入的用户名或口令不正确、\n请重新输入!" , "错误提示" , MessageBoxButtons.OK , MessageBoxIcon.Information );
            }
        }

        private void btnModify_Click( object sender , EventArgs e )
        {
            if ( tbUserId.Text.ToLower( ) == "eflong" && tbPassword.Text == "eflong" )
            {
                //mainForm.cbDemo.Visible = true;
                //mainForm.cbAutoUpload.Visible = true;
                this.Close( );
                ParasForm form = new ParasForm( );
                form.mainForm = mainForm;
                ThreadPool.QueueUserWorkItem( this.ShowParasForm , form );
            }
            else if ( tbUserId.Text.ToLower( ) == "admin" && tbPassword.Text == "admin" )
            {
                this.Close( );
                if ( mainForm.kjSysUserControl2 != null )
                {
                    ParasForm2 form = new ParasForm2( );
                    form.mainForm = mainForm;

                    //mainForm.cbDemo.Visible = false;
                    ThreadPool.QueueUserWorkItem( this.ShowParasForm , form );
                }
                else
                {
                    ParasForm1 form = new ParasForm1( );
                    form.mainForm = mainForm;

                    //mainForm.cbDemo.Visible = false;
                    ThreadPool.QueueUserWorkItem( this.ShowParasForm , form );
                }
            }
            else
            {
                MessageBox.Show( this , "您输入的用户名或口令不正确、\n请重新输入!" , "错误提示" , MessageBoxButtons.OK , MessageBoxIcon.Information );
            }
        }

        private void ShowParasForm( object state )
        {
            ParasForm form = ( ParasForm )state;
            form.StartPosition = FormStartPosition.Manual;
            form.Location = new Point( 280 , 200 );
            DialogResult result = form.ShowDialog( );
        }

        private void tbUserId_TextChanged( object sender , EventArgs e )
        {
        }
    }
}